﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace xycn.pluginsupport.ControllerPlugins
{
    /// <summary>
    /// 本次请求的相关信息
    /// </summary>
    public class RequestInfo
    {
        /// <summary>
        /// 根据请求体生成的流对象
        /// </summary>
        public Stream bodyStream = null;
        /// <summary>
        /// 请求长度
        /// </summary>
        public long? contentLength = null;
        /// <summary>
        /// 请求类型
        /// </summary>
        public string contentType = "";

        /// <summary>
        /// 请求中包含的的cookie键值对列表
        /// </summary>
        public List<KeyValuePair<string, string>> cookieList = new List<KeyValuePair<string, string>>();

        /// <summary>
        /// 请求中包含的form键值对列表
        /// </summary>
        public List<KeyValuePair<string, string>> formDataList = new List<KeyValuePair<string, string>>();

        /// <summary>
        /// HTTP 请求方案。
        /// </summary>
        public string scheme = "";

        /// <summary>
        /// 标识Form内容的类型
        /// </summary>
        public bool hasFormContentType = false;

        /// <summary>
        /// 是否有json类型的内容
        /// </summary>
        public bool hasJsonContentType = false;

        /// <summary>
        /// http请求头字典
        /// </summary>
        public List<KeyValuePair<string, string>> headersList = new List<KeyValuePair<string, string>>();

        /// <summary>
        /// 服务器
        /// </summary>
        public string host = "";

        /// <summary>
        /// 是否采用https
        /// </summary>
        public bool isHttps = false;

        /// <summary>
        /// 请求方法
        /// </summary>
        public string requestMethod = "";

        /// <summary>
        /// 请求路径
        /// </summary>
        public string path = "";

        /// <summary>
        /// 基路径
        /// </summary>
        public string pathBase = "";

        /// <summary>
        /// 请求采用的协议
        /// </summary>
        public string protocol = "";

        public List<KeyValuePair<string, string>> queryList = new List<KeyValuePair<string, string>>();

        /// <summary>
        /// 请求中查询字符串
        /// </summary>
        public string queryString = "";

        /// <summary>
        /// 路由值
        /// </summary>
        public Dictionary<string, object> routeValues = new Dictionary<string, object>();
    }

    /// <summary>
    /// 本次请求的相关信息
    /// </summary>
    public class ResponseInfo
    {
        /// <summary>
        /// 响应内容长度，默认为空，当为空时不处理
        /// </summary>
        public long? ContentLength = null;
        /// <summary>
        /// 响应内容类型，默认为空，当为空时不处理
        /// </summary>
        public string ContentType = null;

        /// <summary>
        /// 为响应信息新增cookie，默认为空，当为空时不处理。
        /// newCoo
        /// </summary>
        public List<KeyValuePair<string, string>> newCookies = null;

        /// <summary>
        /// 删除响应消息中的某些cookie设置，默认为空，当为空时不处理。
        /// deleteCookies列表中存放需要清除的cookie名字
        /// </summary>
        public List<string> deleteCookies = null;

        /// <summary>
        /// 响应头信息，当为空时不处理。
        /// newHeaders列表中，存放需要新增的Header名称及值
        /// </summary>
        public List<KeyValuePair<string, string>> newHeaders = null;

        /// <summary>
        /// 响应头信息，默认为空，当为空时不处理。
        /// deleteHeaders列表中，存放需要删除的Header名称
        /// </summary>
        public List<string> deleteHeaders = null;

        /// <summary>
        /// 重定向地址，默认为空，当为空时不处理
        /// </summary>
        public string redirectLocation = null;

        /// <summary>
        /// 回应状态码，默认为空，当为空时不处理
        /// </summary>
        public int? StatusCode = null;
    }

    /// <summary>
    /// 封装框架HttpContext中常用的数据，以便支撑Controller能够获取更为丰富的信息
    /// </summary>
    public class ControllerContext
    {
        #region ====== http交互连接信息 ======
        public string remoteIP = "";
        public int remotePort = 0;
        public string localIP = "";
        public int localPort = 0;
        public IDictionary<object, object?> TranItems = new Dictionary<object, object?>();
        public X509Certificate2 clientCertificate = null;
        #endregion

        /// <summary>
        /// 请求的唯一编号
        /// </summary>
        public string requestIdentifier = "";

        #region ====== Request信息 ======
        /// <summary>
        /// 本次请求环境中的请求信息
        /// </summary>
        public RequestInfo requestInfo = new RequestInfo();
        #endregion

        #region ====== Response信息======
        /// <summary>
        /// 本次请求环境中的回应信息。
        /// Controller进行设置后，会由框架程序自动进行处理
        /// </summary>
        public ResponseInfo responseInfo = new ResponseInfo();
        #endregion
    }
}
